package czStvDpds.mapeditor;

import czStvDpds.mapmanagment.Map;
import czStvDpds.mapmanagment.MapLoader;
import czStvDpds.mapmanagment.Tile;
import czStvDpds.mapmanagment.TilesInfo;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;

/**
 * Spustí okna která od uživatele zjistí informace o nahráni/vytvoření mapy,
 * mapu nahraje/vytvoří a spustí ediotr
 *
 * @author Jirka
 */
public class MapEditorLauncher {

    public static void main(String[] args) {
        new MapEditorLauncher().run();
    }

    public void run() {
        new StartFrame().start();
    }

    class StartFrame extends JFrame {

        public void start() {
            initFrame();
            initComponents();
            printWarningMessage();
            this.setVisible(true);
        }

        public void initFrame() {
            this.setTitle("DPDS Map editor");
            this.setSize(450, 299);
            this.setLocation(400, 200);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(new GridLayout(2, 1));
        }
        JButton loadButton, createButton;

        public void initComponents() {
            loadButton = new JButton("Nahrát mapu");
            loadButton.setFocusable(false);
            loadButton.addActionListener(new LoadButtonAL());
            this.add(loadButton);
            createButton = new JButton("Vytvořit mapu");
            createButton.setFocusable(false);
            createButton.addActionListener(new CreateButtonAL());
            this.add(createButton);
        }

        public void printWarningMessage() {
            System.out.println("WARNING: V map ediotru je nyní bug, když zmáčknete F10 tak se dlaždice\n"
                    + "sice nastaví na spawn ale potom je při pohybu myši štětec zaseklý.\n"
                    + "Prosím používejte výběr na spodní liště.");
        }

        class LoadButtonAL implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new LoadMapFrame().start();
            }
        }

        class CreateButtonAL implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new CreateMapFrame().start();
            }
        }
    }

    class LoadMapFrame extends JFrame {

        public void start() {
            initFrame();
            initComponents();
            this.setVisible(true);
        }

        public void initFrame() {
            this.setTitle("DPDS Map Editor");
            this.setSize(500, 249);
            this.setLocation(400, 250);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(new GridLayout(3, 1));
        }
        JLabel mapNameLabel;
        JTextField mapNameTextField;
        JButton loadMapButton;

        public void initComponents() {
            mapNameLabel = new JLabel("Název souboru mapy ve složce \"resources\\maps\" bez koncovky \".txt\":", JLabel.CENTER);
            this.add(mapNameLabel);
            mapNameTextField = new JTextField("testMap3");
            mapNameTextField.addActionListener(new LoadMapButtonAL());
            this.add(mapNameTextField);
            loadMapButton = new JButton("Nahrát mapu");
            loadMapButton.addActionListener(new LoadMapButtonAL());
            this.add(loadMapButton);
        }

        public void showMapLoadingExceptionMessage() {
            JOptionPane.showMessageDialog(this, "Mapa s tímto názvem nenalezena", "Chyba při nahrávání mapy", JOptionPane.ERROR_MESSAGE);
        }

        class LoadMapButtonAL implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                Map map = null;
                try {
                    map = MapLoader.loadMap("resources/maps/" + mapNameTextField.getText() + ".txt");
                } catch (IOException ex) {
                    System.out.println("Nepodarilo se nahrat mapu");
                }
                if (map != null) {
                    new MapEditorFrame().start(map);
                    dispose();
                } else {
                    showMapLoadingExceptionMessage();
                }
            }
        }
    }

    class CreateMapFrame extends JFrame {

        public void start() {
            initFrame();
            initComponents();
            this.setVisible(true);
        }

        public void initFrame() {
            this.setTitle("DPDS Map editor");
            this.setSize(700, 180);
            this.setLocation(315, 250);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(new BorderLayout());
        }
        JPanel midPanel;
        JLabel mapWidthLabel, mapHeightLabel, fillWithLabel;
        JTextField mapWidthTextField, mapHeightTextField;
        JComboBox fillWithComboBox;
        JButton createMapButton;

        public void initComponents() {
            midPanel = new JPanel(new GridLayout(3, 2));
            this.add(midPanel, BorderLayout.CENTER);
            mapWidthLabel = new JLabel("Šířka mapy:", JLabel.CENTER);
            midPanel.add(mapWidthLabel);
            mapWidthTextField = new JTextField("50");
            midPanel.add(mapWidthTextField);
            mapHeightLabel = new JLabel("Výška mapy:", JLabel.CENTER);
            midPanel.add(mapHeightLabel);
            mapHeightTextField = new JTextField("50");
            midPanel.add(mapHeightTextField);
            fillWithLabel = new JLabel("Dlaždice, kterou bude mapa na začátku vyplněná", JLabel.CENTER);
            midPanel.add(fillWithLabel);
            fillWithComboBox = new JComboBox();
            fillWithComboBox.setMaximumRowCount(TilesInfo.getTilesInfo().getTilesNumber());
            for (int i = 1; i <= TilesInfo.getTilesInfo().getTilesNumber(); i++) {
                fillWithComboBox.addItem(TilesInfo.getTilesInfo().getName(i));
            }
            midPanel.add(fillWithComboBox, BorderLayout.WEST);
            createMapButton = new JButton("Vytvořit mapu");
            createMapButton.addActionListener(new CreateMapButtonAL());
            this.add(createMapButton, BorderLayout.SOUTH);
        }

        class CreateMapButtonAL implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 1; i <= TilesInfo.getTilesInfo().getTilesNumber(); i++) {
                    String name = TilesInfo.getTilesInfo().getName(i);
                    if (name.equals(fillWithComboBox.getSelectedItem())) {
                        dispose();
                        new MapEditorFrame().start(createMap(Integer.parseInt(mapWidthTextField.getText()),
                                Integer.parseInt(mapHeightTextField.getText()),
                                TilesInfo.getTilesInfo().getTile(name)));
                    }
                }
            }
        }
    }

    public Map createMap(int width, int height, Tile fillWith) {
        Map newMap = new Map(width, height);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                newMap.setTile(x, y, fillWith);
            }
        }

        return newMap;
    }
}
